package com.zpark.controller;

import com.zpark.common.Response;
import com.zpark.entity.Member;
import com.zpark.service.impl.MemberServiceImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2024-03-18
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Resource
    private MemberServiceImpl memberService;
    @RequestMapping("/login")
    public Response login(@RequestBody Member member, HttpSession session){
        Member loginMember = memberService.login(member);
        session.setAttribute("MEMBER",loginMember);
        if (loginMember!=null){
            return Response.success().setData(loginMember).setMessage("登录成功");
        }else
            return Response.failure().setMessage("登录失败");
    }
    @PostMapping("/register")
    public Response register(@RequestBody Member member){
        int res = memberService.register(member);
        if (res==0)
            return Response.failure().setMessage("注册失败");
        else
            return Response.success();
    }
    @RequestMapping("/upload")
    public Response upload(@RequestParam("file")MultipartFile fileF){
        String filename = fileF.getOriginalFilename();
        //创建一个新的文件对象
        File file = new File("F:/t_resume/",filename);
        //访问地址
        String path=  "/upload/"+filename;
        //写入新的文件
        try {
            fileF.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Response.success(path);
    }
    @RequestMapping("/selectByName/{name}")
    public Response selectByName(@PathVariable String name){
        Member member1 = memberService.selectByUsname(name);
        System.out.println(member1);
        if (member1==null)
            return Response.success();
        else
            return Response.failure().setMessage("用户名已存在");
    }
}
